
ifndef TILERA_ROOT
$(error The 'TILERA_ROOT' environment variable is not set.)
endif

CC = $(TILERA_ROOT)/bin/tile-cc
AR = $(TILERA_ROOT)/bin/tile-ar
#CFLAGS += -DLWIP_DEBUG
ARFLAGS=rs
TOPDIR=../../../../..

#Set this to where you have the lwip core module checked out from CVS
#default assumes it's a dir named lwip at the same level as the contrib module
LWIPDIR=$(TOPDIR)/lwip/lwip/src
MCHDIR=$(TOPDIR)/lwip/contrib/ports/mch
+OPT = -Os

CFLAGS += $(CPPFLAGS) -I$(LWIPDIR)/include -I.              \
     -I$(LWIPDIR)/include/ipv4 -I$(MCHDIR)/include

# For debug with gdb
CFLAGS += -static -std=gnu99 -Wall -Werror -g $(OPT)

# COREFILES, CORE4FILES: The minimum set of files needed for lwIP.
COREFILES=$(LWIPDIR)/core/mem.c             \
    $(LWIPDIR)/core/memp.c              \
    $(LWIPDIR)/core/netif.c             \
    $(LWIPDIR)/core/pbuf.c              \
    $(LWIPDIR)/core/raw.c               \
    $(LWIPDIR)/core/stats.c             \
    $(LWIPDIR)/core/sys.c               \
    $(LWIPDIR)/core/tcp.c               \
    $(LWIPDIR)/core/tcp_in.c            \
    $(LWIPDIR)/core/tcp_out.c           \
    $(LWIPDIR)/core/udp.c               \
    $(LWIPDIR)/core/dhcp.c              \
    $(LWIPDIR)/core/init.c              \
    $(LWIPDIR)/core/def.c               \
    $(LWIPDIR)/core/timers.c

CORE4FILES=$(LWIPDIR)/core/ipv4/icmp.c          \
    $(LWIPDIR)/core/ipv4/ip.c           \
    $(LWIPDIR)/core/ipv4/inet.c         \
    $(LWIPDIR)/core/ipv4/ip_addr.c          \
    $(LWIPDIR)/core/ipv4/ip_frag.c          \
    $(LWIPDIR)/core/ipv4/inet_chksum.c

# NETIFFILES: Files implementing various generic network interface functions.'
NETIFFILES=$(LWIPDIR)/netif/etharp.c     \
     $(MCHDIR)/netif/tile_if.c           \
     $(MCHDIR)/netif/if_netio_api.c

# LWIPFILES: All the above.
LWIPFILES=$(COREFILES) $(CORE4FILES) $(NETIFFILES)
LWIPOBJS=$(LWIPFILES:.c=.o)

LWIPLIB=liblwipv4.a

APPSRCS=mch_main.c server.c 
APPOBJS=$(APPSRCS:.c=.o)
APPEXEC=http_server

.PHONY: all depend dep compile clean

all: $(LWIPLIB) $(APPOBJS) depend
	@echo CC $(APPEXEC)
	@$(CC) $(CFLAGS) $(APPOBJS) -L. -llwipv4 -lnetio -ltmc -o $(APPEXEC)

%.o:%.c
	@$(CC) $(CFLAGS) -c $< -o $@
	@echo  CC  $(notdir $<)

clean:
	@rm -f *.o $(LWIPLIB) .depend* $(APPEXEC)
	@find $(LWIPDIR) -name *.o -exec rm {} \;

$(LWIPLIB): $(LWIPOBJS)
	@$(AR) $(ARFLAGS) $(LWIPLIB) $?

depend: .depend

.depend: $(LWIPFILES)
	@$(CC) $(CFLAGS) -MM $^ > .depend || rm -f .depend

-include .depend
